package com.biye.api.controller.sftp;

import com.alibaba.fastjson.JSONObject;
import com.biye.api.service.SFTPService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Author Maple.Chen
 * @Description 管理sftp账户的对外交互处理器，
 * @Date 15:59 2021/3/25
 **/

@Controller
@RequestMapping("sftp")
public class SFTPController {

    // 已在WebMvcConfig注入
    @Resource
    private RestTemplate restTemplate;

    @Value("${python_api.host:}")
    // 加上半角：号，若没有该配置属性，则变量值为"" 【"".equals(contextPath)为true】
    private String serviceHost;
    @Value("${python_api.sftp_api_port:}")
    private String sftp_service_port;
    @Value("${python_api.encrypt_port:}")
    private String encrypt_port;

    @Resource
    private SFTPService sftpService;

    @RequestMapping(value = "sftp_user_mag", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addOrDelSftpAccount(@RequestBody Map<String, Object> map)
    {
        // 定义提供服务的真实接口地址
        String url = serviceHost + ":" + sftp_service_port + "/sftp_user_mag";
        // 请求头封装
        HttpHeaders headers = new HttpHeaders();
        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_JSON
        headers.setContentType(MediaType.APPLICATION_JSON);
        //RestTemplate带参传的时候要用HttpEntity<?>对象传递
        HttpEntity<Map<String, Object>> payload = new HttpEntity<Map<String, Object>>(map, headers);
        /*
            通过填充载荷等参数发起Http请求。
            由于服务器端自定义的响应格式为String形式的JSON串，所以用ResponseEntity<String>.getBody()接收
         */
        ResponseEntity<String> response = restTemplate.postForEntity(url, payload, String.class);
        String body = response.getBody();
        JSONObject res = JSONObject.parseObject(body);
        return sftpService.manageSFTPAccount(map, res);
    }

    @RequestMapping(value = "list_sftp", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String querySFTPAccount(@RequestBody Map<String, Object> map){
        return sftpService.querySFTPAccount(map);
    }

    // query own-account.  查询当前职工用户自己的sftp账号信息
    @RequestMapping(value = "list_staff_sftp", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JSONObject queryAllSftpAccountOfStaff(@RequestBody Map<String, Object> map){
        return sftpService.queryAllSftpAccountOfStaff(map);
    }

    // 借助sftp账号查询路径下的文件
    @RequestMapping(value = "sftp_nodes", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JSONObject queryTreeNodesInfo(@RequestBody Map<String, Object> requestMap) {
        return sftpService.queryTreeNodesInfo(requestMap);
    }

    // SFTP下载文件
    @RequestMapping(value = "download_file", method = RequestMethod.POST)
    public void sftpDownload(@RequestBody Map<String, Object> requestMap, HttpServletResponse response){
        sftpService.sftpDownload(requestMap, response);
    }

}
